using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/* OWED */
using OWED.Document;
using OWED.Network;
using OWED.Network.Messages;
using OWED.Network.Messages.General;
using OWED.Network.Messages.Reply;

public partial class ViewDocumentList : System.Web.UI.Page {

	/// <summary>
	/// Document list request
	/// </summary>
	GetDocumentList request;

	/// <summary>
	/// Backend connection
	/// </summary>
	Connection conn;

	/// <summary>
	/// Page output
	/// </summary>
	protected Literal pageOutput;

	/// <summary>
	/// Username and password
	/// </summary>
	String[] AuthDetails;

	/// <summary>
	/// Document list reply
	/// </summary>
	IProtocolItem reply;

	/// <summary>
	/// Document list
	/// </summary>
	DocumentListReply Documents;

	protected void Page_Load(object sender, EventArgs e)
	{
		pageOutput.Text = "";

		/* check if user is logged in */
		if (!IsLoggedIn.Check(Request.Cookies["owed"])) {
			Response.Redirect("/Login.aspx");
		} else {
			AuthDetails = IsLoggedIn.GetAuthentication(Request.Cookies["owed"]);
		}

		// perform login
		conn = null;
		WebLogin.C(out conn, AuthDetails[0], AuthDetails[1]);
		request = new GetDocumentList(new User(AuthDetails[0]));
		reply = MessageGet.GetFromXml(conn.Send(request.ToXml()));

		if (reply as PassFail != null) {
			pageOutput.Text = "<h2>Error</h2> Getting document list failed";
			return;
		}

		// printo out document list
		pageOutput.Text = "<h2>Wiki Document List</h2><ul>";
		Documents = (DocumentListReply)reply;
		foreach (DocName d in Documents.DocumentNames)
			pageOutput.Text += "<li><a href=\"ViewDocument.aspx?document=" + d.name + "\">" + d.name + "</a></li>";
		pageOutput.Text += "</ul>";
	}
}
